﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SignalProcessLib
{
    public class OverlapTool
    {
        /// <summary>
        /// 将多组原始时域数据拼接成连续的时域数据
        /// </summary>
        /// <param name="data">待合并的原始时域数据</param>
        /// <param name="overlap">重叠率</param>
        /// <returns></returns>
        public static float[] PackRawData(List<float[]> data, float overlap)
        {
            float dataLen = data[0].Length;//单层长度
            int count = data.Count;//层数
            float overlapNum;//单层数组重复率
            float[,] rawDataArray = new float[count, (int)dataLen];//二维的输出数组
            float rawLen = dataLen;//一维数组长度累计
            overlapNum = dataLen * overlap;//重叠数

            //首层赋值
            for (int i = 0; i < dataLen; i++)
            {
                rawDataArray[0, i] = data[0][i];
            }

            //去掉重叠部分
            for (int i = 1; i < count; i++)
            {
                for (int j = 0; j < dataLen - overlapNum; j++)
                {
                    rawDataArray[i, j] = data[i][j + (int)overlapNum];
                }
                rawLen = rawLen + dataLen - overlapNum;
            }

            float[] rawData = new float[(int)rawLen];//一维输出数组
            //二维数组转换成一维数组
            int k = 0;
            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < dataLen; j++)
                {
                    if (rawDataArray[i, j] == 0) continue;
                    rawData[k] = rawDataArray[i, j];
                    k++;
                }
            }
            return rawData;
        }

        /// <summary>
        /// 基于重叠率和采样长度，计算下发给硬件的真实采样长度
        /// </summary>
        /// <param name="sampleLen">用户设定的采样长度</param>
        /// <param name="overlap">用户设定的重叠率</param>
        /// <returns></returns>
        public static int GetRealSampleLen(int sampleLen, float overlap)
        {
            float realSampleLen = sampleLen * overlap;
            return (int)realSampleLen;
        }
    }
}
